// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.commons.ui.swt.web;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.w3c.dom.Document;

import dengues.system.HTMLWebHelper;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class URLDialog extends Dialog {

    public final static String CHANGE_SELECTION_NODE = "CHANGE_SELECTION_NODE"; //$NON-NLS-1$

    private String url;

    private Composite container;

    private final String title;

    private HTMLStructureComposite structureComposite;

    private HTMLBrowserComposite browserComposite;

    private PropertyChangeListener listener;

    /**
     * Qiang.Zhang.Adolf@gmail.com URLDialog constructor comment.
     * 
     * @param parentShell
     */
    public URLDialog(Shell parentShell, String url, String title) {
        super(parentShell);
        setShellStyle(SWT.RESIZE | SWT.APPLICATION_MODAL | SWT.MAX | SWT.MIN);
        this.url = url;
        this.title = title;
        createPropertyListener();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createPropertyListener".
     */
    private void createPropertyListener() {
        listener = new PropertyChangeListener() {

            /*
             * (non-Javadoc)
             * 
             * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
             */
            public void propertyChange(final PropertyChangeEvent evt) {
                browserComposite.printNodeMark(evt.getNewValue());
            }
        };
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#close()
     */
    @Override
    public boolean close() {
        if (structureComposite != null) {
            structureComposite.removePropertyChangeListener(listener);
        }
        return super.close();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        container = (Composite) super.createDialogArea(parent);
        container.setLayout(new GridLayout());
        SashForm mainForm = new SashForm(container, SWT.HORIZONTAL);
        mainForm.setLayout(new GridLayout());
        mainForm.setLayoutData(new GridData(GridData.FILL_BOTH));
        createHTMLStructure(mainForm);
        createHTMLBrowser(mainForm);
        mainForm.setWeights(new int[] { 1, 3 });
        getShell().setText(title);
        return container;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createHTMLStructure".
     * 
     * @param mainForm
     */
    private void createHTMLStructure(Composite mainForm) {
        structureComposite = new HTMLStructureComposite(mainForm);
        createNodeTree();
        structureComposite.addPropertyChangeListener(listener);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createHTMLBrowser".
     * 
     * @param mainForm
     */
    private void createHTMLBrowser(Composite mainForm) {
        browserComposite = new HTMLBrowserComposite(mainForm, url);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createNodeTree".
     */
    private void createNodeTree() {
        try {

            Document parseDOM = HTMLWebHelper.parseDOM((new URL(url)).openStream());
            structureComposite.setInput(parseDOM);
        } catch (MalformedURLException e) {
            MessageDialog.openError(getParentShell(), "Malformed URL Error", e.getMessage()); //$NON-NLS-1$
        } catch (IOException e) {
            MessageDialog.openError(getParentShell(), "IO Error", e.getMessage()); //$NON-NLS-1$
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setSize(900, 400);

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed() {
        url = browserComposite.getURL();
        super.okPressed();
    }

    public String getUrl() {
        return this.url;
    }
}
